// by Xeno
#define __DEBUG__
#define THIS_FILE "x_shcinit.sqf"
#include "x_setup.sqf"

__TRACE("x_shcinit");

if !(call FUNC(checkSHC)) exitWith {};

GVAR(allunits_add) = [];
GVAR(allunits_ai_add) = [];
GVAR(delvecsmt) = [];
if (GVAR(doRespawnGroups)) then {
	GVAR(reduce_groups) = [];
};
GVAR(no_more_observers) = false;
GVAR(counterattack) = false;
GVAR(create_new_paras) = false;
GVAR(x_sm_rem_ar) = [];
GVAR(x_sm_vec_rem_ar) = [];
GVAR(check_trigger) = objNull;
GVAR(first_time_after_start) = true;
GVAR(nr_observers) = 0;
GVAR(sm_resolved) = false;
GVAR(main_target_ready) = false;
GVAR(mt_spotted) = false;
// TYRGHEN - Needed for the priority target
GVAR(priority_trigger) = objNull;
GVAR(priority_objects) = [];
GVAR(priority_resolved) = false;

if (GVAR(IS_HC_CLIENT)) then {
	__ccppfln(x_shc\x_f\x_shcfunctions.sqf);
	__ccppfln(i_server.sqf);
};

execFSM "fsms\NotAliveRemover.fsm";
if (isServer) then {
	execFSM "fsms\NotAliveRemoverUnits.fsm";
};
execFSM "fsms\GroupClean.fsm"; // TODO: Check if this can also run server only

if (GVAR(doRespawnGroups)) then {
	execFSM "fsms\ReduceGroups.fsm";
};

if (GVAR(IS_HC_CLIENT)) then {
	[QGVAR(cgrd), {
		private ["_side", "_type", "_pos", "_dgrp", "_unit_array"];
		PARAMS_3(_side,_type,_pos);
		_dgrp = [_side] call FUNC(creategroup);
		_unit_array = [_type, _side] call FUNC(getunitliste);
		[_pos, _unit_array select 0, _dgrp,true] call FUNC(makemgroup);
		[_dgrp, _pos] call FUNC(taskDefend);
	}] call FUNC(NetAddEventSTO);
	
	[QGVAR(spcr), {
		private ["_side", "_vec", "_dgrp"];
		PARAMS_2(_side,_vec);
		_vec addEventHandler ["killed", {_this call FUNC(handleDeadVec)}];
		_dgrp = [_side] call FUNC(creategroup);
		[_vec, _dgrp] call FUNC(spawnCrew);
		_vec lock true;
		__addDead(_vec)
	}] call FUNC(NetAddEventSTO);
	
	[QGVAR(cgraa), {
		private ["_side", "_vect", "_dgrp","_vecs"];
		PARAMS_2(_side,_vect);
		_vecs = [];
		for "_io" from 1 to 2 do {
			_mmm = format [QGVAR(base_anti_air%1), _io];
			_mpos = markerPos _mmm;
			_mpos set [2,0];
			_mdir = markerDir _mmm;
		
			_dgrp = [_side] call FUNC(creategroup);
			_ret = [1, _mpos, _vect, _dgrp, 0, _mdir] call FUNC(makevgroup);
			((_ret select 0) select 0) lock true;
			// TYRGHEN
			_vec = ((_ret select 0) select 0);
			(group _vec) setBehaviour "COMBAT";
			(group _vec) setCombatMode "RED";
			(driver _vec) disableAI "MOVE";
			_vec addEventHandler ["HandleDamage",{0}];
			{ _x addEventHandler ["HandleDamage",{0}];
			  _x setSkill 1;
			} foreach crew _vec;
			_vecs set[count _vecs, _vec];
		};
		_vecs spawn { private "_vecs"; _vecs = _this; while {{alive _x} count _vecs > 0} do { {[_x] call FUNC(reloadInstant);} foreach _vecs; sleep 347.345; }};
	}] call FUNC(NetAddEventSTO);
};

#ifndef __TT__
if (GVAR(WithRecapture) == 0 && {GVAR(MissionType) != 2}) then {execFSM "fsms\Recapture.fsm"};

if (!GVAR(no_sabotage) && {GVAR(MissionType) != 2}) then {execFSM "fsms\Infilrate.fsm"};
#endif

// start air AI (KI=AI) after some time
#ifndef __TT__
if (GVAR(MissionType) != 2) then {
	0 spawn {
		scriptName "spawn_x_shcinit_airki";
		sleep 30;
		if (isServer && {!isNil QGVAR(HC_CLIENT_OBJ)}) exitWith {};
		sleep 880;
		["KA",GVAR(number_attack_choppers)] execVM "x_shc\x_airki.sqf";
		sleep 801.123;
		["SU",GVAR(number_attack_planes)] execVM "x_shc\x_airki.sqf";
	};
};
#endif

if (count GVAR(with_isledefense) > 0) then {execVM "x_shc\x_isledefense.sqf"};